package bbs.net;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bbs.model.Submit;

/**
 * Created by nimi on 2015/11/14.
 */
public class MyPost {
    Download download;
    Delegate delegate;
    Upload upload;
    public MyPost(Upload upload){
        this.upload=upload;
    }

    public Upload getUpload() {
        return upload;
    }

    public void setUpload(Upload upload) {
        this.upload = upload;
    }

    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }
    public Delegate getDelegate() {
        return delegate;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }
    public MyPost(){
        super();
    }

    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/bbs/MyTieZi";
        RequestQueue requestQueue = VolleySingleton.getVolleySingleton(MyApplication.share()).getRequestQueue();
        StringRequest stringRequest =new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            public void onResponse(String response) {
                afterGet(response);
                if(delegate!=null)
                    delegate.onResponse(response,MyPost.this);
            }}, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error, MyPost.this);

            }
        })

        {
            @Override
            protected Map<String, String> getParams () {
                Map map = new HashMap();
                map.put("data",upload.getUserId());
                return map;
            }
            @Override
            protected Response<String> parseNetworkResponse (NetworkResponse response){
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }

        };
        requestQueue.add(stringRequest);
    }
    private void afterGet(String response) {
        download = JSON.parseObject(response, Download.class);
    }
    // 上行数据
    public static class Upload
    {
        // 用户帐号
        String userName;
        // 用户id
        String userId;
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getUserId() {
            return userId;
        }
        public void setUserId(String userId1) {
            this.userId = userId1;
        }


    }

    // 下行数据
    public static class Download
    {
        // 状态标记
        int status;

        // 我的所有帖子
        List<Submit> ties = new ArrayList<Submit>();

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<Submit> getTies() {
            return ties;
        }

        public void setTies(List<Submit> ties) {
            this.ties = ties;
        }


    }
    public static interface Delegate
    {
        public void onResponse(String response, MyPost myPost);
        public void onErrorResponse(VolleyError error, MyPost myPost);
    }

}
